﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO_DTO_WS.DAO;
using DAO_DTO_WS.DTO;

namespace testPre.WebUserControl
{
    public partial class Login : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] == null || Session["ID"].ToString() == "")
            {
                pnlLogin.Visible = true;
                pnlLogout.Visible = false;
                lblUser.Text = "";
            }
            else
            {
                pnlLogin.Visible = false;
                pnlLogout.Visible = true;

                lblUser.Text = Session["HoTen"].ToString();
            }
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string user = txtUsername.Text.Trim();
            ThanhVienDTO thanhvien;
            thanhvien =  ThanhVienDAO.LayThanhVien(user);
            if (thanhvien.TinhTrang == -1)
            {
                lblError.Text = "Username khong ton tai";
                return;
               
            }
            else            
            {
                if (string.Compare(txtPassword.Text, thanhvien.MatKhau, false) == 0)
                {
                    Session["ID"] = thanhvien.TenThanhVien;
                    Session["HoTen"] = thanhvien.HoTen;
                }
                else
                {
                    lblError.Text = "Sai mat ma hoac sai ten dang nhap";
                    return;
                }
            }
            Response.Redirect("DanhSachSanPham.aspx");

        }
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Session.Clear();

            Response.Redirect("DanhSachSanPham.aspx");
        }
        protected void btnSignup_Click(object sender, EventArgs e)
        {
            Response.Redirect("DangKy.aspx");
        }
    }
}